package com.eshop.utils;

import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.data.domain.Page;
import java.util.ArrayList;
import java.util.List;

public class PageUtil extends cn.hutool.core.util.PageUtil
{
    public static List toPage(final int page, final int size, final List list) {
        final int fromIndex = page * size;
        final int toIndex = page * size + size;
        if (fromIndex > list.size()) {
            return new ArrayList();
        }
        if (toIndex >= list.size()) {
            return list.subList(fromIndex, list.size());
        }
        return list.subList(fromIndex, toIndex);
    }
    
    public static Map<String, Object> toPage(final Page page) {
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", page.getContent());
        map.put("totalElements", page.getTotalElements());
        return map;
    }
    
    public static Map<String, Object> toPage(final Object object, final Object totalElements) {
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", object);
        map.put("totalElements", totalElements);
        return map;
    }
}
